package router

import (
	"crow-qin/app/config"
	"crow-qin/app/models"
	"crow-qin/app/pkg"
	"crow-qin/app/service"
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/toolkits/pkg/logger"
)

type rUser struct {
	service.SysUser
}

func (u *rUser) user() gin.HandlerFunc {

	return func(c *gin.Context) {
		var j service.JWTClaims
		idAuth, myName, err := j.Verify(c)
		//idAuth, myName, err := service.Verify(c)
		if err != nil {
			SetErrDefault(c, err)
			//c.Abort()
			return
		}
		if idAuth == false {
			err = errors.New("身份认证失败")
			SetErrDefault(c, err)
			//c.Abort()
			return
		}
		userInfo, err := models.GetUserByUserName(myName, config.MyTx)
		if err != nil {
			SetErrDefault(c, err)
			return
		}
		logger.Info(userInfo)

		c.Set("user_role", userInfo.Role)
		c.Set("user_name", userInfo.UserName)
		c.Set("user_id", userInfo.ID)
		c.Next()
	}

}

// UpdateUserInfo 用户修改自己信息
// @Summary 用户修改自己信息
// @Description 用户修改自己信息
// @Tags user
// @Accept  application/json
// @Product application/json
// @Param data body UpdateUserInfoRequest  true "选项可选"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/user/info [put]
// @Security Bearer
func (u *rUser) UpdateUserInfo(c *gin.Context) {
	var request UpdateUserInfoRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	userIdAny, _ := c.Get("user_id")

	//更新数据库
	u.ID = userIdAny.(int64)
	u.Email = request.Email
	u.Phone = request.Phone
	err = u.SysUser.Update()
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "用户信息修改成功")
}

type UpdateUserInfoRequest struct {
	Phone string `json:"phone"`
	Email string `json:"email"`
}

// UpdateUserPassword 用户修改密码
// @Summary 用户修改密码
// @Description 用户修改自己的密码
// @Tags user
// @Accept  application/json
// @Product application/json
// @Param data body UpdateUserPasswordRequest  true "用户的新密码和旧密码"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/user/password [put]
// @Security Bearer
func (u *rUser) UpdateUserPassword(c *gin.Context) {
	var request UpdateUserPasswordRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	userIdAny, _ := c.Get("user_id")
	userId := userIdAny.(int64)

	//验证新密码要求
	err = pkg.CheckPassword(request.NewPassword)
	if err != nil {
		SetErrDefault(c, err)
		return
	}

	//旧密码认证
	u.ID = userId
	err = u.SysUser.Get()
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	if u.Password != pkg.EncryptWithMd5(request.Password) {
		err = errors.New("密码验证错误")
	}
	//更新密码
	u.Password = pkg.EncryptWithMd5(request.NewPassword)
	u.ID = userId
	err = u.Update()
	if err != nil {
		SetErrDefault(c, err)
		return
	}

	SetOK(c, "密码修改成功")
}

type UpdateUserPasswordRequest struct {
	//UserId      int64  `json:"user_id"`
	Password    string `json:"password"`
	NewPassword string `json:"new_password"`
}

// GetUserInfo 用户获取本人信息
// @Summary 用户获取本人信息
// @Description 用户获取本人信息
// @Tags user
// @Success 200 {object} response.Response{data=rUser} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/user/info [get]
// @Security Bearer
func (u *rUser) GetUserInfo(c *gin.Context) {
	userIdAny, _ := c.Get("user_id")
	userId := userIdAny.(int64)
	u.ID = userId
	err := u.SysUser.Get()
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, u)

}
